/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/26 15:41:10
 */
Ext.define('Monkey.view.role.RoleController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-role',

    init: function () {

    },

    reloadRole: function () {
        var store = Ext.getCmp('roleGrid').getStore();
        store.loadPage(1);
        this.getReferences().name.setValue('');
        this.getReferences().role.setValue('');
    },

    //显示新增窗口
    addRole: function () {
        var window = Ext.create('Monkey.view.role.RoleWindow');
        window.setTitle('新增角色');
        window.setIcon('resources/icons/16/tag_blue_add.png');
        window.getViewModel().data.isAdd = true;
        window.show();
    },

    //显示修改窗口
    modifyRole: function (record) {
        if (Ext.isEmpty(record.data)) {
            var roleGrid = this.getReferences().roleGrid;
            var records = roleGrid.getSelectionModel().getSelection();
            if (records.length == 0) {
                Ext.Msg.show({
                    title: '提示',
                    message: '请先选中需要修改的角色',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
                return;
            }
            record = records[0];
        }

        var window = Ext.create('Monkey.view.role.RoleWindow');
        window.setTitle('修改角色');
        window.setIcon('resources/icons/16/tag_blue_edit.png');

        var viewModel = window.getViewModel();
        viewModel.data.isAdd = false;
        viewModel.setModelData(record.data);

        window.show();

    },

    removeRole: function () {
        var me = this;
        var roleGrid = this.getReferences().roleGrid;
        var records = roleGrid.getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选中需要删除的角色',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        Ext.Msg.show({
            title: '请确认',
            message: '确认要删除已选中的角色吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function (buttonId) {
                if (buttonId == 'ok') {
                    var ids = Monkey.Utils.arrayModelToString(records, 'id', ',');
                    me.request(Monkey.URL.SERVER_URL + '/m/role/' + ids + '.json', 'DELETE', null, function (data) {
                        var icon = Ext.Msg.ERROR;
                        if (data.success) {
                            icon = Ext.Msg.INFO;
                            me.reloadRole();
                        }
                        Ext.Msg.show({
                            title: '提示',
                            message: data.message,
                            buttons: Ext.Msg.OK,
                            icon: icon
                        });
                    });
                }
            },
            icon: Ext.Msg.INFO
        });
    },

    rowdblclick: function (node, record) {
        this.modifyRole(record);
    },

    ok: function () {
        var view = this.getView(),
            viewModel = this.getViewModel(),
            method = 'PUT',
            me = this;
        var form = view.getReferences().form;
        if (!form.isValid()) {
            return;
        }
        if (viewModel.data.isAdd) {
            method = 'POST';
        }
        this.request(Monkey.URL.SERVER_URL + '/m/role.json', method, viewModel.data, function (data) {
            Ext.Msg.show({
                title: '提示',
                message: data.message,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.INFO
            });
            if (data.success) {
                view.close();
                me.reloadRole();
            }
        });

    },

    cancel: function () {
        this.getView().close();
    },

    //查询按钮相应事件
    findRole: function () {
        var references = this.getReferences();
        var name = references.name.getValue();
        var role = references.role.getValue();
        var store = Ext.getCmp('roleGrid').getStore();
        store.loadPage(1, {
            params: {'search.name_like': name, 'search.role_like': role}
        });
    },

    showGrantPermissionWin: function (cmp) {
        var record = cmp.getWidgetRecord();
        var panel = Ext.create('Monkey.view.permission.GrantPermissionPanel', {
            moduleParam: 'roleId',
            moduleValue: record.data.id,
            getMenuPermissionURL: Monkey.URL.SERVER_URL + '/m/role/menu/permission.json',
            grantRecoveryPermissionURL: Monkey.URL.SERVER_URL + '/m/role/menu/permission.json'
        });
        Ext.create('Ext.window.Window', {
            title: '给角色授权（名称：' + record.data.name + '）',
            icon: 'resources/icons/16/spellcheck.png',
            width: 600,
            height: 500,
            layout: 'fit',
            closable: true,
            resizable: true,
            constrain: true,
            constrainHeader: true,
            modal: true,
            frameHeader: false,
            frame: true,
            maximizable: false,
            border: false,
            items: [panel]
        }).show();
    },

    viewPermission: function () {
        var roleGrid = this.getReferences().roleGrid;
        var records = roleGrid.getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选中需要查看的角色',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        var record = records[0];
        Ext.create('Monkey.view.role.ViewRolePermissionWindow', {
            roleId: record.data.id,
            icon: 'resources/icons/16/eye.png',
            title: '查看角色已分配的权限(' + record.data.name + ')'
        }).show();
    }

});
